﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using IPAWSProxyLib;
using IPAWSProxyLib.IPAWS_EDXL_Svc;
using EDXLSharp.EDXLDELib;
using System.Collections.Specialized;
using EDXLSharp.CAPLib;


namespace IPAWSTester
{
  class Program
  {
    EDXLProxy edxlTester;
    CAPProxy capTester;
    IPAWS_CAP12Proxy cap12Tester;

    static void Main(string[] args)
    {
      Program testRun = new Program();

      //testRun.TestEDXL();

      //Console.WriteLine("");

      //testRun.TestCAP();

      //Console.WriteLine("");

      testRun.TestCAP12();

      Console.Read();
    }

    private void TestCAP()
    {
      Console.WriteLine("Starting CAP 1.1 Tests");
      Console.WriteLine("-----------------------");
      Console.WriteLine("");

      capTester = new CAPProxy(100014, "DMOPEN");

      Console.WriteLine("Try Pinging IPAWS Server");
      if (capTester.TryPing())
      {
        Console.WriteLine("Try Ping Succeeded");
      }
      else
      {
        Console.WriteLine("Try Ping Failed");
      }
      Console.WriteLine("");

      Console.WriteLine("Pinging IPAWS Server");
      try
      {

        capTester.Ping();
        Console.WriteLine("Ping Succeeded");
      }
      catch (Exception Ex)
      {
        if (Ex.InnerException != null)
        {
          Console.WriteLine("Ping Failed - " + Ex.InnerException.ToString());
        }
        else
        {
          Console.WriteLine("Ping Failed - " + Ex.ToString());
        }
      }

      Console.WriteLine("");

      //////////////////////////////
      //StreamReader sr = new StreamReader(@"<some cap 1.1 file>");
      //CAP cap = null;

      //try
      //{
      //  cap = new CAP(sr.ReadToEnd());
      //}
      //finally
      //{
      //  sr.Close();
      //}

      //if (cap != null)
      //{
      //  Console.WriteLine("Posting CAP to IPAWS Server");

      //  try
      //  {
      //    //capTester.PostMessage(cap);
      //    Console.WriteLine("Post Succeeded");
      //  }
      //  catch (Exception Ex)
      //  {
      //    if (Ex.InnerException != null)
      //    {
      //      Console.WriteLine("Post Failed - " + Ex.InnerException.ToString());
      //    }
      //    else
      //    {
      //      Console.WriteLine("Post Failed - " + Ex.ToString());
      //    }
      //  }
      //}
      //Console.WriteLine("");

      //////////////////////////////
      Console.WriteLine("Try Getting Message List");

      List<NameValueCollection> messages = null;

      if (capTester.TryGetMessageList("status", "Test", "equalto", ref messages))
      {
        Console.WriteLine("Get Succeeded");

        if (messages != null)
        {
          foreach (NameValueCollection item in messages)
          {
            if (item.AllKeys.Contains<string>("msgid"))
            {
              Console.WriteLine("msgid = " + item.Get("msgid"));
            }

            if (item.AllKeys.Contains<string>("sender"))
            {
              Console.WriteLine("sender = " + item.Get("sender"));
            }

          }
        }
      }
      else
      {
        Console.WriteLine("Get Message List Failed");
      }
      Console.WriteLine("");

      Console.WriteLine("Getting Message List");
      try
      {
        messages = capTester.GetMessageList("sender", "dmcgarry", "equalto");
        if (messages != null)
        {
          Console.WriteLine("Got " + messages.Count.ToString() + " Messages");
          foreach (NameValueCollection item in messages)
          {
            if (item.AllKeys.Contains<string>("msgid"))
            {
              Console.WriteLine("msgid = " + item.Get("msgid"));
            }

            if (item.AllKeys.Contains<string>("sender"))
            {
              Console.WriteLine("sender = " + item.Get("sender"));
            }

          }
        }
      }
      catch (Exception Ex)
      {
        if (Ex.InnerException != null)
        {
          Console.WriteLine("Get Message List Failed - " + Ex.InnerException.ToString());
        }
        else
        {
          Console.WriteLine("Get Message List Failed - " + Ex.ToString());
        }
      }

      Console.WriteLine("");
      ////////////////////////////
      List<CAP> capMessages = null;

      Console.WriteLine("Try Getting Messages - cap_047_0703_2");
      if (capTester.TryGetMessages("cap_047_0703_2", "gham@grandpaham.com", ref capMessages))
      {
        Console.WriteLine("Get Succeeded - returned " + (capMessages != null ? capMessages.Count.ToString() : "0") + " messages");
      }
      else
      {
        Console.WriteLine("Get Failed");
      }
      Console.WriteLine("");

      Console.WriteLine("Getting Messages - 778f4fee-2323-43b0-8254-e7415a3c913a");
      try
      {
        capMessages = capTester.GetMessages("778f4fee-2323-43b0-8254-e7415a3c913a", null); //"gham@grandpaham.com");
        Console.WriteLine("Get Succeeded - returned " + (capMessages != null ? capMessages.Count.ToString() : "0") + " messages");
      }
      catch (Exception Ex)
      {
        Console.Write("Get Messages Failed - ");

        while (Ex != null)
        {
          Console.WriteLine(Ex.Message + "-" + Ex.Source);
          Ex = Ex.InnerException;
        }
      }
      Console.WriteLine("");

      Console.WriteLine("Getting Messages - greaterthan 2011-09-07T16:55:49-04:00");
      try
      {
        capMessages = capTester.GetMessages("sent", "2011-09-07T16:55:49-04:00", "greaterthan");
        Console.WriteLine("Get Succeeded - returned " + (capMessages != null ? capMessages.Count.ToString() : "0") + " messages");
      }
      catch (Exception Ex)
      {
        Console.Write("Get Messages Failed - ");

        while (Ex != null)
        {
          Console.WriteLine(Ex.Message + "-" + Ex.Source);
          Ex = Ex.InnerException;
        }
      }
      Console.WriteLine("");
      Console.WriteLine("Please press Enter to continue...");

      Console.Read();
    }

    private void TestCAP12()
    {
      Console.WriteLine("Starting CAP 1.2 Tests");
      Console.WriteLine("-----------------------");
      Console.WriteLine("");

      //cap12Tester = new IPAWS_CAP12Proxy(100014, "DMOPEN");
      cap12Tester = new IPAWS_CAP12Proxy(120074, "IPAWSOPEN");

      //Console.WriteLine("Try Pinging IPAWS Server");
      //if (cap12Tester.TryPing())
      //{
      //  Console.WriteLine("Try Ping Succeeded");
      //}
      //else
      //{
      //  Console.WriteLine("Try Ping Failed");
      //}
      //Console.WriteLine("");

      //Console.WriteLine("Pinging IPAWS Server");
      //try
      //{

      //  cap12Tester.Ping();
      //  Console.WriteLine("Ping Succeeded");
      //}
      //catch (Exception Ex)
      //{
      //  if (Ex.InnerException != null)
      //  {
      //    Console.WriteLine("Ping Failed - " + Ex.InnerException.ToString());
      //  }
      //  else
      //  {
      //    Console.WriteLine("Ping Failed - " + Ex.ToString());
      //  }
      //}

      //Console.WriteLine("");

      //////////////////////////////
      StreamReader sr = new StreamReader(@"alert.xml");
      CAP cap = null;

      try
      {
        cap = new CAP(sr.ReadToEnd());
      }
      finally
      {
        sr.Close();
      }

      if (cap != null)
      {
        Console.WriteLine("Posting CAP to IPAWS Server");

        try
        {
          List<NameValueCollection> returnParams = null;

          if (cap12Tester.PostMessage(cap, ref returnParams))
          {
            Console.WriteLine("Post Succeeded");
            Console.WriteLine("");
            Console.WriteLine("Returned parameter lists - ");
            if (returnParams != null)
            {
              int parameterListIndex = 1;
              foreach (NameValueCollection nvc in returnParams)
              {
                Console.WriteLine("ParamList " + parameterListIndex.ToString());

                int parameterIndex = 1;
                foreach (string key in nvc.AllKeys)
                {
                  if (key != null)
                  {
                    Console.WriteLine("param " + parameterIndex.ToString() + " - " + key);

                    string val = nvc.Get(key);
                    if (val != null)
                    {
                      Console.WriteLine("value " + parameterIndex.ToString() + " - " + val);
                    }
                    parameterIndex++;
                  }
                  Console.WriteLine(key + " - " + nvc.Get(key));
                }
                parameterListIndex++;
              }
            }
          }
          else
          {
            Console.WriteLine("Post Failed");
          }
        }
        catch (Exception Ex)
        {
          if (Ex.InnerException != null)
          {
            Console.WriteLine("Post Failed - " + Ex.InnerException.ToString());
          }
          else
          {
            Console.WriteLine("Post Failed - " + Ex.ToString());
          }
        }
      }
      Console.WriteLine("");

      //////////////////////////////
      Console.WriteLine("Try Getting Message List, status == Test");

      List<NameValueCollection> messages = null;

      if (cap12Tester.TryGetMessageList("status", "Test", "equalto", ref messages))
      {
        Console.WriteLine("Get Succeeded");

        if (messages != null)
        {
          Console.WriteLine("Got " + messages.Count.ToString() + " Messages");

          foreach (NameValueCollection item in messages)
          {
            if (item.AllKeys.Contains<string>("msgid"))
            {
              Console.WriteLine("msgid = " + item.Get("msgid"));
            }

            if (item.AllKeys.Contains<string>("sender"))
            {
              Console.WriteLine("sender = " + item.Get("sender"));
            }

          }
        }
      }
      else
      {
        Console.WriteLine("Get Message List Failed");
      }
      Console.WriteLine("");

      //Console.WriteLine("Getting Message List, sender == dmcgarry");
      //try
      //{
      //  messages = cap12Tester.GetMessageList("sender", "dmcgarry", "equalto");
      //  if (messages != null)
      //  {
      //    Console.WriteLine("Got " + messages.Count.ToString() + " Messages");
      //    foreach (NameValueCollection item in messages)
      //    {
      //      if (item.AllKeys.Contains<string>("msgid"))
      //      {
      //        Console.WriteLine("msgid = " + item.Get("msgid"));
      //      }

      //      if (item.AllKeys.Contains<string>("sender"))
      //      {
      //        Console.WriteLine("sender = " + item.Get("sender"));
      //      }

      //    }
      //  }
      //}
      //catch (Exception Ex)
      //{
      //  if (Ex.InnerException != null)
      //  {
      //    Console.WriteLine("Get Message List Failed - " + Ex.InnerException.ToString());
      //  }
      //  else
      //  {
      //    Console.WriteLine("Get Message List Failed - " + Ex.ToString());
      //  }
      //}

      Console.WriteLine("");
      ////////////////////////////
      List<CAP> capMessages = null;

      //Console.WriteLine("Try Getting Messages - identifier == cap12_003_0831_1 and sender == gham@grandpaham.com");
      //if (cap12Tester.TryGetMessages("cap12_003_0831_1", "gham@grandpaham.com", ref capMessages))
      //{
      //  Console.WriteLine("Get Succeeded - returned " + capMessages.Count.ToString() + " messages");
      //}
      //else
      //{
      //  Console.WriteLine("Get Failed");
      //}
      //Console.WriteLine("");

      //Console.WriteLine("Getting Messages - identifier == cap12_003_0831_1");
      //try
      //{
      //  capMessages = cap12Tester.GetMessages("cap12_003_0831_1", null); //"gham@grandpaham.com");
      //  Console.WriteLine("Get Succeeded - returned " + (capMessages != null ? capMessages.Count.ToString() : "0") + " messages");
      //}
      //catch (Exception Ex)
      //{
      //  Console.Write("Get Messages Failed - ");

      //  while (Ex != null)
      //  {
      //    Console.WriteLine(Ex.Message + "-" + Ex.Source);
      //    Ex = Ex.InnerException;
      //  }
      //}
      //Console.WriteLine("");

      //Console.WriteLine("Getting Messages - greaterthan 2011-09-30T16:55:49-04:00");
      Console.WriteLine("Getting Messages - sender == w-nws.webmaster@noaa.gov");
      try
      {
        //capMessages = ipawsCAP12Tester.GetMessages("sent", "2011-09-30T16:55:49-04:00", "greaterthan");
        capMessages = cap12Tester.GetMessages("sender", "w-nws.webmaster@noaa.gov", "equalto");
        if (capMessages != null)
        {
          foreach (CAP msg in capMessages)
          {
            Console.WriteLine("identifier: " + msg.MessageID + ", Sender: " + msg.SenderID + ", Sent: " + msg.SentDateTime.ToString());
          }

          Console.WriteLine("Get Succeeded  - returned " + (capMessages != null ? capMessages.Count.ToString() : "0") + " messages");
        }
        else
        {
          Console.WriteLine("Get returned a null message list");
        }
      }
      catch (Exception Ex)
      {
        Console.Write("Get Messages Failed - ");

        while (Ex != null)
        {
          Console.WriteLine(Ex.Message + "-" + Ex.Source);
          Ex = Ex.InnerException;
        }
      }
      Console.WriteLine("");
      Console.WriteLine("Please press Enter to exit...");

      Console.Read();
    }

    private void TestEDXL()
    {
      edxlTester = new EDXLProxy(999, "DMOPEN");

      Console.WriteLine("Starting EDXL Tests");
      Console.WriteLine("-----------------------");
      Console.WriteLine("");

      Console.WriteLine("Try Pinging IPAWS Server");
      if (edxlTester.TryPing())
      {
        Console.WriteLine("Try Ping Succeeded");
      }
      else
      {
        Console.WriteLine("Try Ping Failed");
      }
      Console.WriteLine("");

      Console.WriteLine("Pinging IPAWS Server");
      try
      {

        edxlTester.Ping();
        Console.WriteLine("Ping Succeeded");
      }
      catch (Exception Ex)
      {
        if (Ex.InnerException != null)
        {
          Console.WriteLine("Ping Failed - " + Ex.InnerException.ToString());
        }
        else
        {
          Console.WriteLine("Ping Failed - " + Ex.ToString());
        }
      }

      Console.WriteLine("");

      ////////////////////////////
      //StreamReader sr = new StreamReader(@"<some EDXL file>");
      //EDXLDE deobj = null;
      //try
      //{
      //  deobj = new EDXLDE(sr.ReadToEnd());
      //}
      //finally
      //{
      //  sr.Close();
      //}

      //if (deobj != null)
      //{
      //  Console.WriteLine("Posting EDXL to IPAWS Server");
      //  try
      //  {
      //    edxlTester.PostMessage(deobj);
        
      //    Console.WriteLine("Post Succeeded");
      //  }
      //  catch
      //  {
      //    Console.WriteLine("Post Failed");
      //  }
      //}
      //Console.WriteLine("");

      //////////////////////////////
      Console.WriteLine("Try Getting Message List");

      List<NameValueCollection> messages = null;

      if (edxlTester.TryGetMessageList("distributionStatus", "Test", "equalto", ref messages))
      {
        Console.WriteLine("Get Succeeded");

        if (messages != null)
        {
          Console.WriteLine("Got " + messages.Count.ToString() + " Messages");
          foreach (NameValueCollection item in messages)
          {
            if (item.AllKeys.Contains<string>("distributionId"))
            {
              Console.WriteLine("distributionId = " + item.Get("distributionId"));
            }

            if (item.AllKeys.Contains<string>("senderId"))
            {
              Console.WriteLine("senderId = " + item.Get("senderId"));
            }

          }
        }
      }
      else
      {
        Console.WriteLine("Get Message List Failed");
      }
      Console.WriteLine("");

      Console.WriteLine("Getting Message List");
      try
      {
        messages = edxlTester.GetMessageList("senderID", "nycoem@icnet.mitre.org", "equalto");
        if (messages != null)
        {
          Console.WriteLine("Got " + messages.Count.ToString() + " Messages");
          foreach (NameValueCollection item in messages)
          {
            if (item.AllKeys.Contains<string>("distributionId"))
            {
              Console.WriteLine("distributionId = " + item.Get("distributionId"));
            }

            if (item.AllKeys.Contains<string>("senderId"))
            {
              Console.WriteLine("senderId = " + item.Get("senderId"));
            }

          }
        }
      }
      catch (Exception Ex)
      {
        if (Ex.InnerException != null)
        {
          Console.WriteLine("Get Message List Failed - " + Ex.InnerException.ToString());
        }
        else
        {
          Console.WriteLine("Get Message List Failed - " + Ex.ToString());
        }
      }

      Console.WriteLine("");
      ////////////////////////////
      List<EDXLDE> edxlMessages = null;

      DateTime end = System.DateTime.UtcNow;
      DateTime start;

      start = end.AddDays(-1); //note that interval should be negative

      string startDate = start.ToLocalTime().ToString("yyyy-MM-ddTHH:mm:sszzz");

      Console.WriteLine("Try Getting Messages - > " + startDate);
      if (edxlTester.TryGetMessages("dateTimeSent", startDate, "greaterthan", ref edxlMessages))
      {
        Console.WriteLine("Get Succeeded - " + (edxlMessages != null ? edxlMessages.Count.ToString() : "0") + " messages");
      }
      else
      {
        Console.WriteLine("Get Failed");
      }
      Console.WriteLine("");

      Console.WriteLine("Getting Messages - NYCOEMTestRMRequest");
      try
      {
        edxlMessages = edxlTester.GetMessages("de-010_2011811123739", "gham@grandpaham.com");

        //edxlMessages = edxlTester.GetMessages("NYCOEMTestRMRequest", "");
        Console.WriteLine("Get Succeeded - returned " + (edxlMessages != null ? edxlMessages.Count.ToString() : "0") + " messages" );
      }
      catch (Exception Ex)
      {
        if (Ex.InnerException != null)
        {
          Console.WriteLine("Get Messages Failed - " + Ex.InnerException.ToString());
        }
        else
        {
          Console.WriteLine("Get Messages Failed - " + Ex.ToString());
        }
      }
      Console.WriteLine("");
      Console.WriteLine("Please press Enter to continue...");

      Console.Read();
    }
  }
}
